<?php
/**
 * Created by PhpStorm.
 * User: jocky
 * Date: 28/09/2017
 * Time: 21:47
 */
namespace backend\controllers;

use common\models\ar\User;
use common\models\ar\UserTrade;
use common\models\Common;
use common\models\MoneyDao;
use common\models\TraderDao;

class TradeCostReturnController extends BackendController
{
    public function actionIndex()
    {
        $level = \Yii::$app->user->identity->level;
        $levelInfo = Common::getLevelsInfo($level);
        $isFirst = $levelInfo['is_first'] == 1 ? true : false;
        $isLast = $levelInfo['is_last'] == 1 ? true : false;

        $time = strtotime(date('Ymd'));
        $from = $time - 7 * 24 * 3600;
        $to = $time + 24 * 3600;

        $data = [
            'is_first' => $isFirst,
            'is_last' => $isLast,
            'from' => date('Y-m-d', $from),
            'to' => date('Y-m-d', $to - 1)
        ];

        return $this->renderPartial('index', $data);
    }

    public function actionInfo()
    {
        $req = \Yii::$app->request;

        $uid = \Yii::$app->user->identity->userID;
        $userModel = User::findOne($uid);

        $level = \Yii::$app->user->identity->level;
        $levelInfo = Common::getLevelsInfo($level);
        $isFirst = $levelInfo['is_first'] == 1 ? true : false;
        $isLast = $levelInfo['is_last'] == 1 ? true : false;

        //日期
        $from = $req->get('time_start', date('Ymd', time() - 7 * 24 * 3600));
        $to = $req->get('time_end', date('Ymd'));
        $from = strtotime($from);
        $to = strtotime($to) + 24 * 3600;

        $clientSN = trim($req->get('clientSN', ''));
        $pClientSN = trim($req->get('p_clientSN', ''));
        $pageNo = (int)$req->get('pageNo', 1);
        $pageSize = \Yii::$app->params['pageSize'];

        $uid = -1;
        if ($clientSN != '') {
            $where['clientSN'] = $clientSN;
            $where['uid_level_' . $userModel->level] = $userModel->uid;
            $tmp = UserTrade::find()->where($where)->one();
            if (!$tmp) {
                $uid = 0;
            } else {
                $uid = $tmp->uid;
            }
        }

        if ($pClientSN) {
            $where = [];
            if ($pClientSN != $userModel->clientSN) {
                $where['clientSN'] = $pClientSN;
            }
            if (!empty($where)) {
                $where['uid_level_' . $userModel->level] = $userModel->uid;
                $tmp = User::find()->where($where)->one();
                if (!$tmp) {
                    $pid = 0;
                    $pLevel = 0;
                } else {
                    $pid = $tmp->uid;
                    $pLevel = $tmp->level;
                }
            } else {
                $pid = $userModel->uid;
                $pLevel = $userModel->level;
            }
        } else {
            $pid = -1;
            $pLevel = -1;
        }

        $session = [
            'uid' => $userModel->uid,
            'is_first' => $isFirst,
            'is_last' => $isLast,
            'level' => $level,
            'level_cid' => $levelInfo['cid']
        ];

        $res = TraderDao::tradeCostReturn($session, $pid, $pLevel, $uid, $from, $to, $pageNo, $pageSize);
        $res['pageNo'] = $pageNo;
        $res['pageTotal'] = ceil($res['count'] / $pageSize);

        exit(json_encode($res));
    }
}
